package com.bole.controller;

import com.bole.data.Result;
import com.bole.data.ResultEnum;
import com.bole.pojo.TbRoleDo;
import com.bole.service.RoleService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yakamaxi
 * @version 1.0
 * @date 2020/8/12 13:36
 */
@RestController
@RequestMapping("role")
@Api("角色管理")
public class RoleController {

    @Autowired
    private RoleService roleService;

    // 查
    @GetMapping
    public Result get() {
        List<TbRoleDo> roleDoList = roleService.getInfo();
        return Result.response(ResultEnum.OK).setData(roleDoList);
    }

    // 增
    @PostMapping
    public Result add(@RequestBody TbRoleDo roleDo, @RequestParam("nodeIds") Long[] nodeIds) {
        roleService.add(roleDo, nodeIds);
        return Result.response(ResultEnum.OK);
    }

    // 根据 id 获取
    @GetMapping("/{id}")
    public Result getRoleById(@PathVariable("id") Long id) {
        TbRoleDo admin = roleService.getRoleById(id);
        return Result.response(ResultEnum.OK).setData(admin);
    }

    // 改
    @PutMapping("/{id}")
    public Result updateById(@PathVariable("id") Long id, @RequestBody TbRoleDo admin, @RequestParam("nodeIds") Long[] nodeIds) {
        admin.setId(id);
        roleService.updateById(admin, nodeIds);
        return Result.response(ResultEnum.OK);
    }

    // 删
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Long id) {
        roleService.deleteById(id);
        return Result.response(ResultEnum.OK);
    }

}
